var Storaged = {};

//begin ListForm
Storaged.ListForm = function(frmId, url, params, title, role) {
	Storaged.ResultStore = new Ext.data.JsonStore({
        root: 'listResult',
        idProperty: 'id',
        totalProperty: 'totalItems',
        fields: ['id', 'name', 'note', { name: 'updateddate', type: 'date', dateFormat: 'c'}, 'updatedby'],
        proxy : new Ext.data.HttpProxy({url : url})
    });
	Storaged.ResultStore.on('beforeload', function(store){
		store.baseParams = {name: Ext.getCmp(frmId+'name').getValue()};
    });
	
	Storaged.CheckAll = new Ext.grid.CheckboxSelectionModel();

	Storaged.GridResult = new Ext.grid.GridPanel({
        store: Storaged.ResultStore,
        trackMouseOver:false,
      
        disableSelection:false,
        loadMask: true,
        id : frmId + 'GridResult',
        columns: [
                  	Storaged.CheckAll,
                  	new Ext.grid.RowNumberer()  
					,{header:  Resource["storaged.name"], width: 100, sortable: true, dataIndex: 'name'}
                  	,{header:  Resource["storaged.note"], width: 100, sortable: true, dataIndex: 'note'}
                  	,{header:  Resource["common.updateddate"], width: 100, sortable: true, dataIndex: 'updateddate', renderer : Ext.util.Format.dateRenderer('d/m/Y H:i:s')}
                  	,{header:  Resource["common.updatedby"], width: 100, sortable: true, dataIndex: 'updatedby'}
                  	
    	        ],
        sm : Storaged.CheckAll,
        columnLines: true,
        stripeRows: true,
        autoWidth : true,
        autoHeight:true,
        iconCls:'icon-grid',
        title: Resource["storaged.gridtitle"],
        viewConfig: {
            forceFit:true,
            enableRowBody:false,
            showPreview:false
        },
        bbar: new Ext.PagingToolbar({
            pageSize: 10,
            store: Storaged.ResultStore,
            displayInfo: true,
            displayMsg: Resource["common.rangepage"],
            emptyMsg: Resource["common.emptypage"]
        }),
	    listeners : {
		    rowdblclick :{fn: function (grid, rowIndex, e) {
		    		if(role == 2){ // cho sửa
			    		var rows = grid.getSelectionModel().getSelections();
			    		if (Ext.getCmp('maintabs-id').getItem('Storaged.EditForm' + rows[0].data.id) != null) {
			    			Ext.getCmp('maintabs-id').getItem('Storaged.EditForm' + rows[0].data.id).show();
			    		}else{
				    		var form = Storaged.EditForm('Storaged.EditForm' + rows[0].data.id, url, {'id' : rows[0].data.id, 'page': 'edit'},title+' - Chi tiết', rows[0].data.id,role);
					    	Ext.getCmp('maintabs-id').add(form).show();
			    	}
		    		}
			    }}
		}
        
    });
	var listForm = new Ext.FormPanel({
	closable:true,
    url: url,
    frame:true,
    title: title,
    bodyStyle:'padding:5px 0px 0',
	id : frmId,
    items: [{
        xtype:'fieldset',
		id: frmId + 'searchfieldset',
        checkboxToggle:false,
        title: Resource["common.searchinfo"],
        autoHeight:true,
        collapsed: false,
        collapsible : true,
        items :[{
        			id:frmId+'name',
					xtype: 'textfield',
					name : 'name',				   
					fieldLabel: Resource["storaged.name"],
					allowBlank:true,				   
					width: 300
            	},
            	{
					xtype: 'hidden',
					name : 'crudaction'
            	}
        ]
    }, Storaged.GridResult],

    tbar: new Ext.Toolbar([{
	        text: Resource["common.btnew"],
	        id: frmId+'btAdd',
	        hidden: true,
	        iconCls:'x-button-add',
	        handler: function(){
	        	if (Ext.getCmp('maintabs-id').getItem('Storaged.EditForm') != null) {
	        		Ext.getCmp('maintabs-id').getItem('Storaged.EditForm').show();
	        	}else{
	        		Ext.getCmp('maintabs-id').add(Storaged.EditForm('Storaged.EditForm', url, null, title+' - Thêm mới',role)).show();
	        	}
	        }
	    } ,{
        text: Resource["common.btdelete"],
        id: frmId+'btDel',
        hidden: true,
        iconCls:'x-button-delete',
        handler: function(){
	    	var rows = Storaged.CheckAll.getSelections();
        	if (rows.length > 0) {
        		Ext.Msg.show({
		       		title: Resource["common.alert"],
	   			 	msg: Resource["common.confirmdelete"],
	   			 	icon: Ext.MessageBox.QUESTION,
	   			 	buttons: Ext.MessageBox.OKCANCEL,
	   			 	fn: function(btn, text) {
	   			 		if(btn == 'ok') {
		   			 		listForm.getForm().setValues([{id : 'crudaction', value : 'delete'}]);
				        	queryStr = FormUtils.toQueryStr('checkList', rows);
				        	
				        	var urlDelete = url;
				        	if (queryStr != '') {
				        		urlDelete += '?' + queryStr;
				        	}
				        	
				            listForm.getForm().submit({
				                url: urlDelete,
				                waitMsg: Resource["common.msgwaiting"],
				                success: function(usergroupForm, o){
				                	Storaged.ResultStore.loadData(o.result);
									if (o.result.messageRespone  != null && o.result.messageRespone != '') {
										Ext.MessageBox.show({
									           title: Resource["common.alert"],
									           msg: o.result.messageResponse,
									           buttons: Ext.MessageBox.OK
									       });
									}					                    
				                }
				           });
	   			 		}
        			}
        		})
	        }else{
	        		Ext.MessageBox.show({
				           title: Resource["common.alert"],
				           msg: Resource["common.mustselectonce"],
				           buttons: Ext.MessageBox.OK
				       });
	        }
	    }
    } ,{
        text: Resource["common.btsearch"],
        iconCls:'x-button-search',
        handler: function(){
    		var searchfieldset = Ext.getCmp(frmId + 'searchfieldset');
	        if(searchfieldset.collapsed) {
	        	searchfieldset.expand(true);
	        	return;
	        }
	    	listForm.getForm().submit({
	            url: url,
	            waitMsg: Resource["common.msgwaiting"],
	            success: function(roleForm, o){
	            	Storaged.ResultStore.loadData(o.result);
					if (o.result.messageRespone  != null && o.result.messageRespone != '') {
						Ext.MessageBox.show({
					           title: Resource["common.alert"],
					           msg: o.result.messageResponse,
					           buttons: Ext.MessageBox.OK
					       });
					}					                    
	            }
	    	});
    	}
        
    } ,{
        text: Resource["common.btreset"],
        iconCls:'x-button-reset',
        handler: function(){
           listForm.getForm().reset();
        }
    } ,{
        text: Resource["common.btclose"],
        iconCls:'x-button-close',
        handler: function(){
        	Ext.getCmp('maintabs-id').remove(listForm, true);
        }
    }])
});
	listForm.getForm().load({
		url : url,
		params : params,
		method : 'POST',
		waitTitle: Resource["common.alert"],
		waitMsg: Resource["common.msgwaiting"],
		success : function(form, action) {
			Storaged.ResultStore.loadData(action.result);
			if(role == 2){
				Ext.getCmp(frmId+'btAdd').show();
				Ext.getCmp(frmId+'btDel').show();
			}
		}, failure : function(frm, action) {doSessionExpired(frm, action);}
	});

	return listForm;
}
// end ListForm

// begin EditForm
Storaged.EditForm = function(frmId, url, params,title,id,role) {
	var form = new Ext.FormPanel({
    labelWidth: 120,
    closable:true,
    id : frmId,
    url:url,
    frame:true,
    title: title,
    bodyStyle:'padding:5px 5px 0',
    
    items: [{
        xtype:'fieldset',
        checkboxToggle:false,
        title: title,
        autoHeight:true,
        defaults: {width: 300},
        defaultType: 'textfield',
        collapsed: false,
        items :[{
	                fieldLabel: Resource["storaged.name"],
	                name: 'name',
	                allowBlank : false
	            },{
	                fieldLabel: Resource["storaged.note"],
	                name: 'note',
	                allowBlank : false
	            },{
	            	xtype : 'hidden',
	            	name : 'id'
	            },{
	            	xtype : 'hidden',
	            	name : 'crudaction',
	            	value : 'insert-update'
	            },{
	            	xtype : 'hidden',
	            	name : 'page',
	            	value : 'edit'
	            }
	        ]
    }],

    tbar: new Ext.Toolbar([{
        text: Resource["common.btsave"],
        iconCls:'x-button-save',
        handler: function(){
	        if(form.getForm().isValid()){
	        	form.getForm().setValues([{id : 'crudaction', value : 'insert-update'}]);
	            form.getForm().submit({
	                url: url,
	                waitMsg: Resource["common.msgwaiting"],
	                success: function(roleForm, o){
	            		if(o.result.messageResponse != null){
	            			Ext.MessageBox.show({
						           title: Resource["common.alert"],
						           msg: o.result.messageResponse,
						           buttons: Ext.MessageBox.OK
						       });
	            		}
	                    if (Storaged.ListForm != null) {
	                    	Storaged.GridResult.getStore().reload();
	                    }
	                }
	            });
	        }
	    }
    },{
        text: Resource["common.btreset"],
        iconCls:'x-button-reset',
        handler: function(){
            if(form.getForm().findField('id').getValue() != ''){
            	Storaged.LoadEditForm();
            }else{
            	form.getForm().reset();
            }
        }
    },{
        text: Resource["common.btback"],
        iconCls:'x-button-back',
        handler: function(){
        	if (Ext.getCmp('maintabs-id').getItem('Storaged.ListForm') != null) {
        		Ext.getCmp('maintabs-id').getItem('Storaged.ListForm').show();
        	}else{
            	Ext.getCmp('maintabs-id').add(Storaged.ListForm('Storaged.ListForm', url, {crudaction : 'search', pg4_offset : 0, maxPageItems : 10}, Resource["storaged.title"],role)).show();
            }
        }
    },{
        text: Resource["common.btclose"],
        iconCls:'x-button-close',
        handler: function(){
            Ext.getCmp('maintabs-id').remove(frmId, true);
        }
    }])
});

	Storaged.LoadEditForm = function(){
	form.getForm().load({
		url : url,
		params : params,
		waitMsg:Resource["common.msgwaiting"],
		success : function(form, action) {
			form.setValues([
						{id : 'id', value : action.result.data.id}
						,{id : 'name', value : action.result.data.name}
						,{id : 'note', value : action.result.data.note}
						
			]);
			
		}
	});
}
	Storaged.LoadEditForm();
return form;

}
//end EditForm